package com.paireach.bw.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RatingBar;

import com.baidu.trace.TraceLocation;
import com.paireach.bw.R;
import com.paireach.bw.application.MyApplication;
import com.paireach.bw.base.BaseFragment;
import com.paireach.bw.bean.LocationInfo;
import com.paireach.bw.bean.ReturnInfo;
import com.paireach.bw.event.AppEvent;
import com.paireach.bw.event.HttpErrorEvent;
import com.paireach.bw.event.HttpSuccessEvent;
import com.paireach.bw.http.RequestTag;
import com.paireach.bw.http.WayBillRequest;
import com.paireach.bw.interface2.LocateCallback;
import com.paireach.bw.utils.BDTraceUtil;
import com.paireach.bw.utils.GsonUtil;

import org.greenrobot.eventbus.EventBus;

import es.dmoral.toasty.Toasty;

/**
 * Created by ${cqc} on 2017/7/31.
 * 待评价
 */

public class AllFrag245 extends BaseFragment implements View.OnClickListener {

    private String orderCode;

    private View rootView;
    private Button btn;
    private Toolbar toolbar;
    private RatingBar rb1;
    private RatingBar rb2;
    private RatingBar rb3;

    public static AllFrag245 getInstance(String orderCode) {
        AllFrag245 frag = new AllFrag245();
        Bundle bundle = new Bundle();
        bundle.putString("orderCode", orderCode);
        frag.setArguments(bundle);
        return frag;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        orderCode = bundle.getString("orderCode");
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.frag_service_evaluation, container, false);
        findViews();
        initViews();
        return rootView;
    }

    private void initViews() {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().onBackPressed();
            }
        });
        toolbar.setTitle("服务评价");
        btn.setOnClickListener(this);
    }

    private void findViews() {
        toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
        rb1 = (RatingBar) rootView.findViewById(R.id.ratingBar1);
        rb2 = (RatingBar) rootView.findViewById(R.id.ratingBar2);
        rb3 = (RatingBar) rootView.findViewById(R.id.ratingBar3);
        btn = (Button) rootView.findViewById(R.id.btn_commit);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_commit:
                final int rating1 = (int) rb1.getRating();
                final int rating2 = (int) rb2.getRating();
                final int rating3 = (int) rb3.getRating();
                if (rating1 != 0 && rating2 != 0 && rating3 != 0) {
                    showProgressDialog();
                    LocationInfo location = ((MyApplication) getActivity().getApplication()).locationInfo;
                    new WayBillRequest().commitServiceEvaluation(phoneNumber, orderCode, rating1, rating2, rating3, location.getLongitude() + "", location.getLatitude() + "", driverNum);
                } else {
                    showToast("请全部评价");
                }
                break;
        }
    }

    @Override
    public void httpSuccessEvent(HttpSuccessEvent event) {
        super.httpSuccessEvent(event);
        dismissProgressDialog();
        if (event.getRequestTag() == RequestTag.COMMIT_SERVICE_EVALUATION) {
            jsonParse(event.getJson());
        }
    }

    private void jsonParse(String json) {
        ReturnInfo info = GsonUtil.jsonToBean(json, ReturnInfo.class);
        showToast(info.getMsg());
        AppEvent event = new AppEvent();
        event.setTag("to_frag3");
        EventBus.getDefault().post(event);
        getActivity().onBackPressed();
    }

    @Override
    public void httpErrorEvent(HttpErrorEvent event) {
        super.httpErrorEvent(event);
        dismissProgressDialog();
        if (event.getRequestTag() == RequestTag.COMMIT_SERVICE_EVALUATION) {
            showToast("提交评价失败");
            AppEvent appEvent = new AppEvent();
            appEvent.setTag("加载失败");
            EventBus.getDefault().post(appEvent);
        }
    }
}
